package com.sls.game.parser.deductrefund;

import java.io.File;
import java.io.InputStream;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import com.sls.core.parser.Parser;
import com.sls.game.pojo.DeductionRefundInfo;

/**
 * 
 * @description 支付退款接口解析
 * @author Wangxuegang
 * @date 2012-10-24
 * 
 */
public class DeductionRefundResParse {

	private static Logger log = Logger.getLogger(DeductionRefundResParse.class);

	/**
	 * 支付退款接口解析
	 * 
	 * @param xmlStr
	 */
	public static DeductionRefundInfo parseDeductRefundRes(String xmlStr) {

		DeductionRefundInfo info = new DeductionRefundInfo();
		InputSource source = null;
		StringReader read = null;
		try {
			// 得到DOM解析器的工厂实例
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory
					.newInstance();
			// 从DOM工厂中获得DOM解析器
			DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder();
			read = new StringReader(xmlStr);
			source = new InputSource(read);

			// 把要解析的xml文档读入DOM解析器
			Document doc = dbBuilder.parse(source);
			Element root = doc.getDocumentElement();
			NodeList childNodes = root.getChildNodes(); // 获得根元素下的子节点
			// 遍历子节点
			for (int i = 0; i < childNodes.getLength(); i++) {
				Node node = childNodes.item(i); // 对每个子节点进行判断
				if ("UserId".equals(node.getNodeName())) { // 第二层节点
					String UserId = node.getTextContent();
					info.setUserNo(UserId);
				} else if ("OrderNo".equals(node.getNodeName())) { // 第二层节点
					String orderNo = node.getTextContent();
					info.setOrderNo(orderNo);
				} else if ("TradeNo".equals(node.getNodeName())) { // 第二层节点
					String tradeNo = node.getTextContent();
					info.setTradeNo(tradeNo);
				} else if ("PaymentFee".equals(node.getNodeName())) { // 第二层节点
					String paymentFee = node.getTextContent();
					info.setPaymentFee(paymentFee);
				} else if ("BusinessType".equals(node.getNodeName())) { // 第二层节点
					String businessType = node.getTextContent();
					info.setBusinessType(businessType);
				} else if ("PaymentStatus".equals(node.getNodeName())) { // 第二层节点
					String paymentStatus = node.getTextContent();
					info.setPaymentStatus(paymentStatus);
				} else if ("ErrorCode".equals(node.getNodeName())) { // 第二层节点
					String errorCode = node.getTextContent();
					info.setErrorCode(errorCode);
				} else if ("ErrorMsg".equals(node.getNodeName())) { // 第二层节点
					String errorMsg = node.getTextContent();
					info.setErrorMsg(errorMsg);
				} else if ("Sign".equals(node.getNodeName())) { // 第二层节点
					String sign = node.getTextContent();
					info.setSign(sign);
				}
			}
		} catch (Exception e) {
			log.error(e);
		}

		return info;
	}

	/**
	 * 读取支付退款配置文件中的信息
	 */
	public static DeductionRefundInfo readConfigurateFile() {
		InputStream xmlStream = Parser.class.getClassLoader()
				.getResourceAsStream(
						"conf" + File.separator + "deductRefund.xml");
		DeductionRefundInfo info = new DeductionRefundInfo();

		try {
			// 得到DOM解析器的工厂实例
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory
					.newInstance();
			// 从DOM工厂中获得DOM解析器
			DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder();

			// 把要解析的xml文档读入DOM解析器
			Document doc = dbBuilder.parse(xmlStream);
			Element root = doc.getDocumentElement();
			NodeList childNodes = root.getChildNodes(); // 获得根元素下的子节点
			// 遍历子节点
			for (int i = 0; i < childNodes.getLength(); i++) {
				Node node = childNodes.item(i); // 对每个子节点进行判断
				if ("address".equals(node.getNodeName())) { // 第二层节点
					String address = node.getTextContent();
					info.setAddress(address);
				} else if ("key".equals(node.getNodeName())) { // 第二层节点
					String key = node.getTextContent();
					info.setKey(key);
				} else if ("balanceAddress".equals(node.getNodeName())) { // 第二层节点
					String balanceAddress = node.getTextContent();
					info.setBalanceAddress(balanceAddress);
				} else if ("balanceKey".equals(node.getNodeName())) { // 第二层节点
					String balanceKey = node.getTextContent();
					info.setBalanceKey(balanceKey);
				}
			}
		} catch (Exception e) {
			log.error(e);
		}

		return info;
	}

	// public static void main(String[] args) {
	// DeductionRefundInfo info = readConfigurateFile();
	// log.info(info.getAddress());
	// log.info(info.getKey());
	// }

}
